package com.memo4android;

import com.memo4android.model.AppModel;
import com.memo4android.model.ItemTodoList;
import com.memo4android.model.Todo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class TodoItem extends Activity implements OnClickListener { 

	 ItemTodoList Tdl;
	 Todo mTodo;
	 AppModel model;
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frame_newtodo);
        model = AppModel.getInstance(this);
        String TodoListName = getIntent().getStringExtra("folderName");
        String TodoName = getIntent().getStringExtra("todoName");
        
        if (model.getTodoListFaible(TodoListName)!=null)
		{
			Tdl = model.getTodoListFaible(TodoListName);
		}
		else if (model.getTodoListMoyennne(TodoListName)!=null)
		{
			Tdl = model.getTodoListMoyennne(TodoListName);
		} 
		else if (model.getTodoListForte(TodoListName)!=null)
		{
			Tdl = model.getTodoListForte(TodoListName);
		}
		

		EditText editText= (EditText) findViewById(R.id.titleNewTodo);
		if(TodoName != null){
			mTodo = Tdl.getTodo(TodoName);
			if (mTodo!= null)
			{
				editText.setText(mTodo.getTitre());
			}
		}
		
        ((Button)this.findViewById(R.id.buttonValidNewTodo)).setOnClickListener((android.view.View.OnClickListener) this);
        ((Button)this.findViewById(R.id.buttonCancelNewTodo)).setOnClickListener((android.view.View.OnClickListener) this);
        
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		
			case R.id.buttonValidNewTodo:
				EditText titleEdit=(EditText) findViewById(R.id.titleNewTodo);
				String title=titleEdit.getText().toString();
				if(title.equals("")){
					//completer avec le message box erreur
				}
				if(mTodo != null)
				{
					mTodo.setTitre(title);
				}
				else
				{
					mTodo = new Todo(title,false);
					Tdl.addTodo(mTodo);
				}
				model.saveTodoToBDD(mTodo);
				Intent intent = new Intent(this, ToDoActivity.class);
				intent.putExtra("PrioriteName", Tdl.getPriorite());
				intent.putExtra("TodoName", Tdl.getTitre());
				this.startActivity(intent);
				break;
				
			case R.id.buttonCancelNewTodo:
				this.finish();
				break;
		}
		
	}
	
		
}
